package com.wj.controller;


import com.wj.pojo.Announcement;
import com.wj.service.IAnnouncementService;
import com.wj.utils.RespUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wj
 * @since 2022-05-16
 */
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {
    @Resource
    private IAnnouncementService announcementService;

    @DeleteMapping("/")
    public RespUtil deleteAnnouncement(@RequestBody Integer[] ids) {
        return announcementService.deleteAnnouncement(ids);
    }

    @GetMapping("/searchAnnouncement")
    public List<Announcement> searchAnnouncement(String searchText){
        return announcementService.searchAnnouncement(searchText);
    }

    @PostMapping("/")
    public RespUtil addAnnouncement(@RequestBody Announcement announcement) {
        if (announcementService.save(announcement)){
            return RespUtil.success("添加成功");
        }
        return RespUtil.error("添加失败");
    }

    @GetMapping("/")
    public List<Announcement> getAllAnnouncement() {
        return announcementService.list();
    }

    @PutMapping("/update")
    public RespUtil updateAnnouncement(@RequestBody Announcement announcement) {
        if (announcementService.updateById(announcement)) {
            return RespUtil.success("更新成功");
        }
        return RespUtil.success("更新失败");
    }
}
